const tokenKey = "access_token";
const customUser = require('/commonUser.js');
const serverDomain = 'http://192.168.3.125:8002/';

//配置不需要传递token的接口
const exceptionAddrArr = ['/api/index/index'];

//处理请求头中token数据
function HandleHeader(url,type){
  //获取缓存中的token
  let token = wx.getStorageSync(tokenKey);
  let header = {};
  if(type == "POST_PARAMS"){
    header = {
      'content-type': 'application/x-www-form-urlencoded',
      'TENANT-ID': '1',
      'Authorization': 'Basic cGlnOnBpZw=='
    }
  }else{
    header = {
      'content-type': 'application/json',
      'TENANT-ID': '1',
      'Authorization': 'Bearer ' + '07644923-36e0-403d-b0f5-2174aea8a9f1'
    }
  }
  //排除请求的地址不需要token的地址
  if (exceptionAddrArr.indexOf(url) == -1) {
    header['token'] = token;
  }
  return header;  
}
//post请求，数据在body中
function postRequest(url, data) {
  let header = HandleHeader(url, 'POST');
  wx.showLoading({
    title: '数据加载中。。。',
  })
  return new Promise((requestSuccess, requestError) => {
    wx.request({
      url: serverDomain + url,
      data: data,
      header: header,
      method: 'POST',
      success: (res => {
        wx.hideLoading();
        if (res.statusCode == 200) {
          requestSuccess(res.data);
        }else if(res.statusCode == 401){
          customUser.clearUserInfo();
          wx.showModal({
            title: '登录提示',
            content: '登录以过期，请重新登录！',
            success: function (res) {
              if (res.confirm) {
                let url = '/pages/users/index/index';
                wx.navigateTo({
                  url: '/pages/users/login/index?url='+url
                })
              }
            }
          })
        }else {
          if(requestError != undefined && requestError != null && requestError != ''){
            requestError(res);
          }else{
            wx.showToast({
              title: '系统错误！',
              icon : 'error',
              duration: '2000',
            })
          }
        }
      }),
      fail: (res => {
        wx.hideLoading();
        if(requestError != undefined && requestError != null && requestError != ''){
          requestError(res);
        }else{
            wx.showToast({
              title: '连接失败',
              icon : 'error',
              duration: '2000',
            })
        }
      })
    })
  })
}

//post请求，数据按照query方式传给后端--url传递参数
function postParamsRequest(url, data) {
  let header = HandleHeader(url, 'POST_PARAMS');
  wx.showLoading({
    title: '数据加载中。。。',
  });
  return new Promise((requestSuccess, requestError) => {
    wx.request({
      url: serverDomain + url,
      data: data,
      header: header,
      method: 'POST',
      success: (res => {
        wx.hideLoading();
        if (res.statusCode == 200) {
          requestSuccess(res.data);
        } else {
          requestError(res);
        }
      }),
      fail: (res => {
        wx.hideLoading();
        requestError(res)
      })
    })
  })
}

//get 请求
function getRequest(url, data) {
  let header = HandleHeader(url, 'GET');
  wx.showLoading({
    title: '数据加载中。。。',
  });
  return new Promise((requestSuccess, requestError) => {
    wx.request({
      url: serverDomain + url,
      data: data,
      header: header,
      method: 'GET',
      success: (res => {
        wx.hideLoading();
        if (res.statusCode == 200) {
          requestSuccess(res.data);
        } else {
          requestError(res);
        }
      }),
      fail: (res => {
        wx.hideLoading();
        requestError(res)
      })
    })
  })
}
//get带域名请求
function getDomainRequest(url, data) {
  let header = HandleHeader(url, 'GET');
  wx.showLoading({
    title: '数据加载中。。。',
  });
  return new Promise((requestSuccess, requestError) => {
    wx.request({
      url:  url,
      data: data,
      header: header,
      method: 'GET',
      success: (res => {
        wx.hideLoading();
        if (res.statusCode == 200) {
          requestSuccess(res.data);
        } else {
          requestError(res);
        }
      }),
      fail: (res => {
        wx.hideLoading();
        requestError(res)
      })
    })
  })
}
module.exports.getRequest = getRequest;
module.exports.postRequest = postRequest;
module.exports.postParamsRequest = postParamsRequest;
module.exports.getDomainRequest = getDomainRequest;
module.exports.serverDomain = serverDomain;